
% convenience function for tracking one high resolution widefield movie.
% Set to work with the wrapper function highResWidefieldWrapper_noGCAMP_v1_1.m
%
% functionally the same as trackOneWidefieldMovie.m but doesn't take any
% gcamp data
function [iosmov,traj] = trackOneWidefieldMovie_iosonly(iosmov,msk,refim,masterRefIm,vesselSkeleton,sessionRegister,micronsPerPixel)

%% Apply window mask:
temp = iosmov( (round(size(iosmov,1)/2)-10):(round(size(iosmov,1)/2)+10),...
    (round(size(iosmov,2)/2)-10):(round(size(iosmov,2)/2)+10),:);
bgval_ios = median(temp(:));
for n = 1:size(iosmov,3)
    iosmov(:,:,n) = iosmov(:,:,n).*msk;
end

iosmov(iosmov==0) = bgval_ios;
refim(~msk) = bgval_ios;
if nargin < 7 || isempty(micronsPerPixel)
    micronsPerPixel = 1/.449;
end


%% run image registration for small motion during the movie
if ~isempty(refim)
    [iosmov,imtrans] = driftCorrectWidefield(iosmov,refim,[],[],"multimodal");
    
    % if have some zero pixels due to shifting the image set those to
    % background levels:
    iosmov(iosmov==0) = bgval_ios;
end

%% run image registration to the master image so can have everything registered across sessions:
if nargin > 5 && ~isempty(sessionRegister)
    f = waitbar(0,'registering to parent session');
    for n = 1:size(iosmov,3)
        iosmov(:,:,n) = imwarp(iosmov(:,:,n),sessionRegister.Transformation,'OutputView',imref2d([size(iosmov,1),size(iosmov,2)]));
        waitbar(n/size(iosmov,3),f);
    end
    close(f)
elseif nargin > 3 && ~isempty(masterRefIm)
    [sessionRegister] = registerMultipleWidefieldImages(refim,masterRefIm);
    f = waitbar(0,'registering to parent session');
    for n = 1:size(iosmov,3)
        iosmov(:,:,n) = imwarp(iosmov(:,:,n),sessionRegister.Transformation,'OutputView',imref2d([size(iosmov,1),size(iosmov,2)]));
        waitbar(n/size(iosmov,3),f);
    end
    close(f)
end
iosmov(iosmov<1) = bgval_ios;
%% once all the image registering is done can run tracking of IOS:
traj = trackWidefieldLinescans_v1_0(iosmov,vesselSkeleton,[],micronsPerPixel);
